/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.com.hydroPK.model.skills.herblore;

import net.com.hydroPK.model.*;
import net.com.hydroPK.model.items.*;
import net.com.hydroPK.*;

/**
 *
 * @author Zack
 */
public class herblore {

    private Player p;
    private PlayerItems Items = new PlayerItems();
    private final int ExperianceRate = 1;

    public herblore(Player p) {
        this.p = p;
    }
    private final int[] Herbs = {249, 251, 253, 255, 257, 2998, 10978, 259, 261, 263, 3000, 265, 2481, 267, 269};
    private final int[] SecondaryItems = {221, 235, 225, 1939, 223, 1550, 1581, 1581, 1975, 239, 1939, 12630, 2152, 9736, 231, 2970, 10111, 225, 241, 223, 10937, 239, 6049, 241, 245, 3138, 247, 6051, 6693};
    private final int[][] GrabUnfinished = {{249, 91}, {251, 93}, {253, 95}, {255, 97}, {257, 99}, {2998, 111}, {10978, 12181}, {259, 101}, {261, 103}, {263, 105}, {3000, 3004}, {265, 107}, {2481, 2483}, {267, 109}, {269, 111}};
    private final int[][] GrabFinished = {{221, 91, 1, 225, 121}, {235, 93, 5, 342, 175}, {225, 95, 12, 450, 115}, {1939, 91, 19, 450, 10142}, {223, 97, 22, 600, 225}, {1550, 127, 22, 600, 7662}, {1581, 97, 25, 720, 1582}, {1975, 97, 26, 884, 3010}, {239, 99, 30, 1050, 133},
        {1939, 93, 31, 750, 10143}, {12630, 91, 31, 720, 12633}, {2152, 95, 34, 960, 3034}, {9736, 97, 38, 1026, 9741}, {231, 99, 38, 1080, 139}, {1939, 95, 39, 750, 10144}, {12109, 12181, 40, 1195, 12142}, {1939, 97, 44, 870, 10145},
        {221, 101, 45, 1200, 145}, {235, 101, 48, 1272, 181}, {231, 103, 50, 1356, 151}, {2970, 103, 52, 1425, 3018}, {10111, 103, 53, 1470, 10000}, {225, 105, 55, 1545, 157}, {241, 105, 60, 1608, 187}, {223, 3002, 63, 1590, 3026},
        {10937, 3024, 65, 1920, 10927}, {239, 107, 66, 1800, 163}, {6049, 111, 68, 1860, 2446}, {241, 2483, 69, 1866, 2454}, {245, 169, 72, 1956, 2454}, {3138, 2483, 76, 2100, 3042}, {247, 1111, 78, 2400, 189}, {6051, 101, 79, 2460, 181},
        {6693, 111, 81, 5000, 6687}};

    private int AloudPot(int SecondaryItem) {
        for (int i = 0; GrabFinished.length > i; i++) {
            if (GrabFinished[i][0] == SecondaryItem) {
                return GrabFinished[i][1];
            }
        }
        return -1;
    }

    private int LevelReq(int SecondaryItem) {
        for (int i = 0; GrabFinished.length > i; i++) {
            if (GrabFinished[i][0] == SecondaryItem) {
                return GrabFinished[i][2];
            }
        }
        return -1;
    }

    private int Experiance(int SecondaryItem) {
        for (int i = 0; GrabFinished.length > i; i++) {
            if (GrabFinished[i][0] == SecondaryItem) {
                return (GrabFinished[i][3]) * ExperianceRate;
            }
        }
        return -1;
    }

    private int GetFinished(int SecondaryItem) {
        for (int i = 0; GrabFinished.length > i; i++) {
            if (GrabFinished[i][0] == SecondaryItem) {
                return (GrabFinished[i][4]) * ExperianceRate;
            }
        }
        return -1;
    }

    private int GetUnfinished(int Herb) {
        for (int i = 0; GrabUnfinished.length > i; i++) {
            if (GrabUnfinished[i][0] == Herb) {
                return GrabUnfinished[i][1];
            }
        }
        return -1;
    }

    private boolean IsHerb(int item) {
        for (int i = 0; Herbs.length > i; i++) {
            if (Herbs[i] == item) {
                return true;
            }
        }
        return false;
    }

    private boolean IsSecondaryItem(int item) {
        for (int i = 0; SecondaryItems.length > i; i++) {
            if (SecondaryItems[i] == item) {
                return true;
            }
        }
        return false;
    }

    private boolean IsWater(int item) {
        return item == 227;
    }

    private int HerbloreLvl() {
        return p.skillLvl[15];
    }

    private void DeleteItem(int item) {
        Engine.playerItems.deleteItem(p, item, Engine.playerItems.getItemSlot(p, item), 1);
    }

    private void AddItem(int item) {
        Engine.playerItems.addItem(p, item, 1);
    }

    private void Message(String Message) {
        p.getActionSender().sendMessage(p, Message);
    }

    private void AppendXp(int Xp) {
        p.appendExperience(Xp, 15);
    }

    private void HerbAnim() {
        p.requestAnim(363, 0);
    }

    public void HandleUnfinished(int ItemUsed, int UsedWith) {
        if (p == null || !IsHerb(ItemUsed) && !IsHerb(UsedWith) || !IsWater(ItemUsed) && !IsWater(UsedWith)) {
            return;
        }
        DeleteItem(ItemUsed);
        DeleteItem(UsedWith);
        HerbAnim();

        if (IsWater(ItemUsed)) {
            AddItem(GetUnfinished(UsedWith));
            Message("You make a " + Engine.items.getItemName(GetUnfinished(UsedWith)) + ".");
        }
        if (IsWater(UsedWith)) {
            AddItem(GetUnfinished(ItemUsed));
            Message("You make a " + Engine.items.getItemName(GetUnfinished(ItemUsed)) + ".");
        }
    }

    public void HandleFinished(int ItemUsed, int UsedWith) {
        if (p == null || !IsSecondaryItem(ItemUsed) && !IsSecondaryItem(UsedWith)) {
            return;
        }

        if (IsSecondaryItem(ItemUsed) && AloudPot(ItemUsed) == UsedWith) {
            if (HerbloreLvl() >= LevelReq(ItemUsed)) {
                AddItem(GetFinished(ItemUsed));
                AppendXp(Experiance(ItemUsed));
                Message("You make a " + Engine.items.getItemName(GetFinished(ItemUsed)) + ".");
                DeleteItem(ItemUsed);
                DeleteItem(UsedWith);
                HerbAnim();
            } else {
                Message("You Need Atleast " + LevelReq(ItemUsed) + " In Herblore To Make This Potion");
            }
        }
        if (IsSecondaryItem(UsedWith) && AloudPot(UsedWith) == ItemUsed) {
            if (HerbloreLvl() >= LevelReq(UsedWith)) {
                AddItem(GetFinished(UsedWith));
                AppendXp(Experiance(UsedWith));
                Message("You make a " + Engine.items.getItemName(GetFinished(UsedWith)) + ".");
                DeleteItem(ItemUsed);
                DeleteItem(UsedWith);
                HerbAnim();
            } else {
                Message("You Need Atleast " + LevelReq(UsedWith) + " In Herblore To Make This Potion.");
            }
        }

    }
}
